{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#创建无穷序列\" data-toc-modified-id=\"创建无穷序列-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>创建无穷序列</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建无穷序列\n",
    "\n",
    "第一个值是由构造函数的参数start（默认为0）指定的，而相邻值之间的差是由参数step（默认为1）指定的。允许用户修改某些元素，这是通过将不符合规则的值保存在字典`changed`中实现的。如果元素未被修改，就使用公式$self.start + key * self.step$来计算它的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s[4]:  9\n",
      "s[4]:  2\n",
      "s[5]:  11\n"
     ]
    }
   ],
   "source": [
    "def check_index(key):\n",
    "    # 检测key的类型\n",
    "    if not isinstance(key, int):\n",
    "        raise TypeError\n",
    "    # 检测key的范围\n",
    "    if key < 0:\n",
    "        raise IndexError\n",
    "        \n",
    "class ArithmeticSequence(object):\n",
    "    def __init__(self, start = 0, step = 1):\n",
    "        \"\"\"\n",
    "        start -序列中的第一个值\n",
    "        step -两个相邻值的差\n",
    "        changed -一个字典，包含用户修改后的值\n",
    "        \"\"\"\n",
    "        self.start = start # 存储起始值\n",
    "        self.step = step # 存储步长\n",
    "        self.changed = {} # 没有任何元素被修改\n",
    "        \n",
    "    def __getitem__(self, key):\n",
    "        \"\"\"\n",
    "        从算术序列中获取一个元素\n",
    "        \"\"\" \n",
    "        check_index(key) # 检测key值是否正确\n",
    "        \n",
    "        try:\n",
    "            return self.changed[key] # 如果该值不存在，则返回一个用公式计算的值\n",
    "        except KeyError:\n",
    "            return self.start + key * self.step\n",
    "        \n",
    "    def __setitem__(self, key, value):\n",
    "        \"\"\"\n",
    "        修改算式序列中的元素\n",
    "        \"\"\"\n",
    "        \n",
    "        check_index(key) # 检测key值是否正确\n",
    "        self.changed[key] = value # 存储修改后的值\n",
    "        \n",
    "s = ArithmeticSequence(start=1, step=2)\n",
    "print(\"s[4]: \", s[4])\n",
    "s[4] = 2 # 给序列设置一个值\n",
    "print(\"s[4]: \", s[4])\n",
    "print(\"s[5]: \", s[5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
